<?php

namespace app\admin\service;

use app\admin\model\UM as UM;
use ShidianUtil\Exception\DataNotExistException;
use app\admin\dao\PromotionGroupFeedbackDao;
use app\common\model\PromotionGroupFeedback;
use think\Collection;

/**
 * Class TestService
 * @package app\admin\service
 */
class TestService
{
    // dao
    private PromotionGroupFeedbackDao $promotionGroupFeedbackDao;

    public function __construct(PromotionGroupFeedbackDao $promotionGroupFeedbackDao)
    {
        $this->promotionGroupFeedbackDao = $promotionGroupFeedbackDao;
    }

    /**
     * 列表
     * @param UM|null $user
     * @param int|null $page
     * @param int|null $page_size
     * @return Collection
     */
    public function index(?UM $user, ?int $page = null, ?int $page_size = null): Collection
    {
        //这里如果要修改查询，建议把查询放到dao层去
        return $this->promotionGroupFeedbackDao->selectAll($page, $page_size, true);
    }

    /**
     * 查询
     * @param UM|null $user
     * @param int $id
     * @return PromotionGroupFeedback
     */
    public function read(?UM $user, int $id): ?PromotionGroupFeedback
    {
        //这里如果要修改查询，建议把查询放到dao层去
        return $this->promotionGroupFeedbackDao->findByPK($id, true);
    }

    /**
     * 新增
     * @param UM $user
     * @param array $params
     * @return  PromotionGroupFeedback
     */
    public function add(UM $user, array $params): PromotionGroupFeedback
    {
        return $this->promotionGroupFeedbackDao->insertActive($params, []);
    }

    /**
     * 更新
     * @param UM  $user
     * @param int   $id
     * @param array $params
     * @return PromotionGroupFeedback
     */
    public function modify(UM $user, int $id, array $params): PromotionGroupFeedback
    {
        //这里如果要修改查询，建议把查询就放到这里
        $record = PromotionGroupFeedback::field(true)->where('id', $id)->find();

        if(is_null($record)){
            throw new DataNotExistException('该对象不存在或已被删除');
        }

        $allowField = [];
        $this->promotionGroupFeedbackDao->updateActive($record, $params, $allowField);

        return $record;
    }

    /**
     * 删除
     * @param UM $user
     * @param int $id
     */
    public function rm(UM $user, int $id): void
    {
        //这里如果要修改查询，建议把查询就放到这里
        $record = PromotionGroupFeedback::field(true)->where('id', $id)->find();

         if (is_null($record)) {
             return;
         }

        $this->promotionGroupFeedbackDao->deleteModel($record);
    }

}
